package com.stm.toolsTest.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.stm.common.annotation.DataScope;
import com.stm.common.core.text.Convert;
import com.stm.common.exception.BusinessException;
import com.stm.common.utils.DateUtils;
import com.stm.toolLedger.domain.KfTools;
import com.stm.toolLedger.mapper.KfToolsMapper;
import com.stm.toolsTest.domain.KfRToolsTest;
import com.stm.toolsTest.domain.KfToolsTestApply;
import com.stm.toolsTest.domain.KfToolsTestInspection;
import com.stm.toolsTest.mapper.KfRToolsTestMapper;
import com.stm.toolsTest.mapper.KfToolsTestApplyMapper;
import com.stm.toolsTest.mapper.KfToolsTestInspectionMapper;
import com.stm.toolsTest.service.KfToolsTestApplyService;

/**
 * 
 * @Description RFIdimpl
 * @CreateTime 2019年4月4日 上午8:43:59
 * @CreateBy 林龙
 */
@Service
public class KfToolsTestApplyServiceImpl implements KfToolsTestApplyService
{
    @Autowired
    private KfToolsTestApplyMapper kfToolsTestApplyMapper;
    @Autowired
    private KfToolsTestInspectionMapper kfToolsTestInspectionMapper;
    @Autowired
    private KfToolsMapper kfToolsMapper;

    /**
     * 根据条件分页查询数据
     * 
     * @param kfTools 信息
     * @return list集合信息
     */
    @Override
    @DataScope(tableAlias = "b3")
    public List<KfToolsTestApply> selectAllPageList(KfToolsTestApply kfToolsTestApply){
        return kfToolsTestApplyMapper.selectAllPageList(kfToolsTestApply);
    }

    /**
     * 根据id查询信息
     * 
     * @param id 
     * @return 对象信息
     */
    @Override
    public KfToolsTestApply selectByPrimaryKey(String id){
        return kfToolsTestApplyMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增保存信息
     * 
     * @param kfToolsTestApply RFID信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insert(KfToolsTestApply kfToolsTestApply){
    	UUID uuid = UUID.randomUUID();
    	kfToolsTestApply.setId(uuid.toString());
    	kfToolsTestApply.setCreateTime(DateUtils.getNowDate());
    	kfToolsTestApply.setStatus("1");
    	
    	for (KfToolsTestInspection kfToolsTestInspection : kfToolsTestApply.getKfToolsTestInspectionList()) {
			
    		String[] ids = Convert.toStringArray(kfToolsTestInspection.getIds());
    		for (String id : ids) {
    			UUID uuid1 = UUID.randomUUID();
				
    			KfTools kfTools =new KfTools();
    			kfTools.setId(id);
    			kfTools.setStatus("3");
    			kfToolsMapper.updateByPrimaryKeySelective(kfTools); //修改库存试验状态
    			
    			kfToolsTestInspection.setId(uuid1.toString());
    			kfToolsTestInspection.setTestId(uuid.toString());
    			kfToolsTestInspection.setToolsId(id);
    			kfToolsTestInspection.setCreateTime(DateUtils.getNowDate());
    			kfToolsTestInspection.setCreateBy(kfToolsTestApply.getCreateBy());
    			kfToolsTestInspectionMapper.insertSelective(kfToolsTestInspection); //新增试验明细表
			}
		}
    	
    	return kfToolsTestApplyMapper.insertSelective(kfToolsTestApply);
    }

	/**
     * 修改保存信息
     * 
     * @param kfToolsTestApply RFID信息
     * @return 结果
     */
    @Override
    @Transactional
    public int update(KfToolsTestApply kfToolsTestApply){
    	
    	kfToolsTestApply.setUpdateTime(DateUtils.getNowDate());
    	
    	
    	String[] kfToolsTestApplyIds = Convert.toStringArray(kfToolsTestApply.getId());
    	kfToolsTestInspectionMapper.deleteByPrimary(kfToolsTestApplyIds); //删除
    	
    	for (KfToolsTestInspection kfToolsTestInspection : kfToolsTestApply.getKfToolsTestInspectionList()) {
			
    		String[] ids = Convert.toStringArray(kfToolsTestInspection.getIds());
    		for (String id : ids) {
    			UUID uuid1 = UUID.randomUUID();
				
    			kfToolsTestInspection.setId(uuid1.toString());
    			kfToolsTestInspection.setTestId(kfToolsTestApply.getId());
    			kfToolsTestInspection.setToolsId(id);
    			kfToolsTestInspection.setUpdateTime(DateUtils.getNowDate());
    			kfToolsTestInspection.setUpdateBy(kfToolsTestApply.getUpdateBy());
    			kfToolsTestInspectionMapper.insertSelective(kfToolsTestInspection); //新增试验明细表
			}
		}
    	
    	return  kfToolsTestApplyMapper.updateByPrimaryKeySelective(kfToolsTestApply);
    }
    
    /**
     * 批量删除
     * 
     * @param ids
     * @throws Exception
     */
    @Override
    public int deleteByPrimary(String ids) throws BusinessException
    {
        String[] kfToolsTestApplyIds = Convert.toStringArray(ids);

        kfToolsTestInspectionMapper.deleteByPrimary(kfToolsTestApplyIds);
        
        return kfToolsTestApplyMapper.deleteByPrimary(kfToolsTestApplyIds);
    }

    /**
     * 
     * @Description 审核
     * @CreateTime 2019年4月11日 下午2:45:46
     * @CreateBy 林龙
     * @param ids
     * @return
     * @throws Exception
     */
	@Override
	public int reviewByPrimary(String ids, String status, String rejectedReason, String userName) throws Exception {
		
		List<KfToolsTestApply> list =new ArrayList<KfToolsTestApply>(); 
		for (String id : Convert.toStringArray(ids)) {
			
			KfToolsTestApply kfToolsTestApply =new KfToolsTestApply();
			kfToolsTestApply.setId(id);
			kfToolsTestApply.setStatus("2");
			list.add(kfToolsTestApply);
		}
		//批量更新
		return kfToolsTestApplyMapper.reviewByPrimary(list);
		  
	}

	/**
	 * 
	 * @Description 试验审核数量
	 * @CreateTime 2019年4月16日 上午8:56:16
	 * @CreateBy 林龙
	 * @return
	 */
	@Override
	@DataScope(tableAlias = "b2")
	public int selectByreviewNums(KfToolsTestApply kfToolsTestApply) {
		return kfToolsTestApplyMapper.selectByreviewNums(kfToolsTestApply);
	}


}
